package view.elements;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.border.TitledBorder;

import model.elements.Field;

@SuppressWarnings("serial")
public class GUI extends JFrame{
	
	public static final int WIDTH = 800;
	public static final int HEIGHT = 450;
	private JPanel my_display;
	private JList my_gear_list;
	private Field my_field;
	private JLabel name;
	private JLabel level;
	private JLabel mp;
	private JLabel my_skill;
	private JLabel pet_name;
	private JLabel perpetuation;
	private JLabel refresh;
	private JLabel my_weather;
	private JLabel day;
	private JLabel siphon;
	
	/**
	 * 
	 */
	public GUI () {
		super("Summoner Simulator");
		my_display = new JPanel(new BorderLayout());
		my_gear_list = new JList();
		my_field = new Field();
		setUpMenu();
		setUpDataPanel();
		setUpButtonPanel();
		setUpDisplay();
		setUpAvatarBar();
	}

	private void setUpButtonPanel() {
		JPanel buttonPanel = new JPanel();
		JButton create = new JButton("New Smn");
		
		create.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent the_event) {
				
				try{
					final String name = JOptionPane.showInputDialog(null, 
							"Enter your name:");
					final int lvl = Integer.parseInt(
							JOptionPane.showInputDialog(null, 
									"Enter your Summoner's Level:\n (Current Range 1:99)"));
					final int mp = Integer.parseInt(
							JOptionPane.showInputDialog(null, 
									"Enter your Summoner's MP:\n (Current Range 0:9999)"));
					my_field.createSmn(name, lvl, mp);
					my_field.Release();
					updateCaller();
					updatePet();
					
					my_gear_list.setListData(my_field.getGearList());
					
				} catch (final IllegalArgumentException e) {
					
					JOptionPane.showMessageDialog(null, e.getMessage());
				}   
		        }	
		});
		create.setPreferredSize(new Dimension(90,30));
		
		JButton changeSkill = new JButton("Smn Skill");
		changeSkill.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				final String skill = JOptionPane.showInputDialog(null,
						"Enter your Summoning Magic Skill\n (Current Range 0:433)");
				
				try{
					
					my_field.changeSkill(Integer.parseInt(skill));
					siphon.setText("Siphon: " + my_field.getSiphon());
					my_skill.setText("Smn Skill: " + my_field.getFinalSkill());
					
				}catch(NumberFormatException e){
					if(skill == null) {//User hit cancel
						//break;
					}else {
						JOptionPane.showMessageDialog(null,
							"Input not a number. Please try again");	
					}
				}catch(NullPointerException e){
					if(skill == null) {
						//user hit cancel
					}else {
						JOptionPane.showMessageDialog(null,"Please create a Summoner before adding skill!");
					}
				}catch(IllegalArgumentException e){
					if(skill == null) {
						//user hit cancel
					}else {
						JOptionPane.showMessageDialog(null,"Value is not in skill range.");
					}
				}
				
			}
			
		});
		changeSkill.setPreferredSize(new Dimension(90,30));
		
		JButton addGear = new JButton("Add Gear");
		addGear.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String gear = JOptionPane.showInputDialog("Enter the name of the gear to equip.\n" +
						"(As it would appear in FFXI: See the Help Menu");
				
				try{
					boolean success = my_field.changeGear(gear.toLowerCase());
				
					my_gear_list.setListData(my_field.getGearList());
					if(my_field.hasPet()) {
						updateCaller();
						updatePet();
						
					}
					if(!success){
						JOptionPane.showMessageDialog(null, "Gear Input Error!\n Please check your spelling!");
					}
				}catch(NullPointerException e){
					if(gear == null) {
						//user hits cancel.
					}else{
						JOptionPane.showMessageDialog(null, "Please create a summoner before adding gear.");
					}
				}
			}
		});
		addGear.setPreferredSize(new Dimension(90,30));
		
		
		buttonPanel.add(create);
		buttonPanel.add(changeSkill);
		buttonPanel.add(addGear);
		
		
		
		my_display.add(buttonPanel, BorderLayout.SOUTH);
	}

	private void setUpDataPanel() {
		JPanel dataPanel = new JPanel(new BorderLayout());
		dataPanel.setPreferredSize(new Dimension(175, 320));
		TitledBorder title1 = BorderFactory.createTitledBorder(
				BorderFactory.createLineBorder(Color.BLACK),"Data");
		dataPanel.setBorder(title1);
		
		JPanel smnPanel = new JPanel(new BorderLayout());
		TitledBorder title2 = BorderFactory.createTitledBorder(
				BorderFactory.createLineBorder(Color.BLACK),"Summoner");
		smnPanel.setBorder(title2);
		JPanel aPanel = new JPanel(new BorderLayout());
		JPanel bPanel = new JPanel(new BorderLayout());
		name = new JLabel();
		name.setText("Name: ");
		level = new JLabel();
		level.setText("Level: ");
		mp = new JLabel();
		mp.setText("Mp: ");
		my_skill = new JLabel();
		my_skill.setText("Smn Skill: ");
		
		
		aPanel.add(name,BorderLayout.NORTH);
		aPanel.add(level,BorderLayout.SOUTH);
		bPanel.add(mp,BorderLayout.NORTH);
		bPanel.add(my_skill,BorderLayout.SOUTH);
		smnPanel.add(aPanel, BorderLayout.NORTH);
		smnPanel.add(bPanel, BorderLayout.SOUTH);
		dataPanel.add(smnPanel, BorderLayout.NORTH);
		
		JPanel petPanel = new JPanel(new BorderLayout());
		TitledBorder title3 = BorderFactory.createTitledBorder(
				BorderFactory.createLineBorder(Color.BLACK),"Pet");
		petPanel.setBorder(title3);
		
		
		pet_name = new JLabel();
		pet_name.setText("Pet Name: ");
		perpetuation = new JLabel();
		perpetuation.setText("Perpetuation: ");
		siphon = new JLabel();
		siphon.setText("Siphon: ");
		refresh = new JLabel();
		refresh.setText("Refresh: ");
		
		
		JPanel panelc = new JPanel(new BorderLayout());
		JPanel paneld = new JPanel(new BorderLayout());
		
		panelc.add(pet_name,BorderLayout.NORTH);
		panelc.add(siphon, BorderLayout.SOUTH);
		paneld.add(perpetuation,BorderLayout.NORTH);
		paneld.add(refresh,BorderLayout.SOUTH);
		petPanel.add(panelc, BorderLayout.NORTH);
		petPanel.add(paneld, BorderLayout.SOUTH);
		dataPanel.add(petPanel, BorderLayout.CENTER);
		
		JPanel weatherPanel = new JPanel(new BorderLayout());
		TitledBorder title4 = BorderFactory.createTitledBorder(
				BorderFactory.createLineBorder(Color.BLACK),"Field");
		weatherPanel.setBorder(title4);
		dataPanel.add(weatherPanel, BorderLayout.SOUTH);
		my_weather = new JLabel();
		my_weather.setText("Weather: \n");
		day = new JLabel();
		day.setText("Day: \n");
		weatherPanel.add(my_weather,BorderLayout.NORTH);
		weatherPanel.add(day,BorderLayout.CENTER);
		
		
		my_display.add(dataPanel, BorderLayout.WEST);
	}

	private void setUpDisplay() {
		
		my_gear_list.setPreferredSize(new Dimension(450,320));
		TitledBorder title1 = BorderFactory.createTitledBorder(
				BorderFactory.createLineBorder(Color.BLACK),"Equipment");
		my_gear_list.setBorder(title1);
		title1.setTitleJustification(TitledBorder.CENTER);
		
		
		my_display.add(my_gear_list, BorderLayout.CENTER);
		
		
		add(my_display, BorderLayout.CENTER);
	}

	/**
	 * Implement & create button functionality to pet command buttons.
	 */
	private void setUpAvatarBar() {
		JPanel avatarBar = new JPanel(new GridLayout(9,2));
		TitledBorder title1 = BorderFactory.createTitledBorder(
				BorderFactory.createLineBorder(Color.BLACK),"Pets");
		avatarBar.setBorder(title1);
		
		avatarBar.setPreferredSize(new Dimension(175,320));
		ArrayList<JButton> buttonList = new ArrayList<JButton>();
		
		JButton ifr = new JButton("Ifrit");
		
		ifr.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try{
					my_field.Ifrit();
					updatePet();
				}catch(NullPointerException ex){
					JOptionPane.showMessageDialog(null, "Please create a Summoner before summoning.");
				}
				
			}
		});
		buttonList.add(ifr);
		JButton tit = new JButton("Titan");
		
		tit.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try{
					my_field.Titan();
					updatePet();
				}catch(NullPointerException ex){
					JOptionPane.showMessageDialog(null, "Please create a Summoner before summoning.");
				}
				
			}
		});
		buttonList.add(tit);
		JButton lev = new JButton("Leviathan");
		
		lev.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try{
					my_field.Leviathan();
					updatePet();
				}catch(NullPointerException ex){
					JOptionPane.showMessageDialog(null, "Please create a Summoner before summoning.");
				}
				
			}
		});
		buttonList.add(lev);
		JButton gar = new JButton("Garuda");
		
		gar.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try{
					my_field.Garuda();
					updatePet();
				}catch(NullPointerException ex){
					JOptionPane.showMessageDialog(null, "Please create a Summoner before summoning.");
				}
				
			}
		});
		buttonList.add(gar);
		JButton shi = new JButton("Shiva");
		
		shi.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try{
					my_field.Shiva();
					updatePet();
				}catch(NullPointerException ex){
					JOptionPane.showMessageDialog(null, "Please create a Summoner before summoning.");
				}
				
			}
		});
		buttonList.add(shi);
		JButton ram = new JButton("Ramuh");
		
		ram.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try{
					my_field.Ramuh();
					updatePet();
				}catch(NullPointerException ex){
					JOptionPane.showMessageDialog(null, "Please create a Summoner before summoning.");
				}
				
			}
		});
		buttonList.add(ram);
		JButton car = new JButton("Carbuncle");
		
		car.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try{
					my_field.Carbuncle();
					updatePet();
				}catch(NullPointerException ex){
					JOptionPane.showMessageDialog(null, "Please create a Summoner before summoning.");
				}
				
			}
		});
		buttonList.add(car);
		JButton fen = new JButton("Fenrir");
		
		fen.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try{
					my_field.Fenrir();
					updatePet();
				}catch(NullPointerException ex){
					JOptionPane.showMessageDialog(null, "Please create a Summoner before summoning.");
				}
				
			}
		});
		buttonList.add(fen);
		JButton dia = new JButton("Diabolos");
		
		dia.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try{
					my_field.Diabolos();
					updatePet();
				}catch(NullPointerException ex){
					JOptionPane.showMessageDialog(null, "Please create a Summoner before summoning.");
				}
				
			}
		});
		buttonList.add(dia);
		JButton fir = new JButton("FireSpirit");
		
		fir.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try{
					my_field.FireSpirit();
					updatePet();
				}catch(NullPointerException ex){
					JOptionPane.showMessageDialog(null, "Please create a Summoner before summoning.");
				}
				
			}
		});
		buttonList.add(fir);
		JButton ear = new JButton("EarthSpirit");
		
		ear.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try{
					my_field.EarthSpirit();
					updatePet();
				}catch(NullPointerException ex){
					JOptionPane.showMessageDialog(null, "Please create a Summoner before summoning.");
				}
				
			}
		});
		buttonList.add(ear);
		JButton wat = new JButton("WaterSpirit");
		
		wat.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try{
					my_field.WaterSpirit();
					updatePet();
				}catch(NullPointerException ex){
					JOptionPane.showMessageDialog(null, "Please create a Summoner before summoning.");
				}
				
			}
		});
		buttonList.add(wat);
		JButton air = new JButton("AirSpirit");
		
		air.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try{
					my_field.AirSpirit();
					updatePet();
				}catch(NullPointerException ex){
					JOptionPane.showMessageDialog(null, "Please create a Summoner before summoning.");
				}
				
			}
		});
		buttonList.add(air);
		JButton ice = new JButton("IceSpirit");
		
		ice.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try{
					my_field.IceSpirit();
					updatePet();
				}catch(NullPointerException ex){
					JOptionPane.showMessageDialog(null, "Please create a Summoner before summoning.");
				}
				
			}
		});
		buttonList.add(ice);
		JButton thu = new JButton("ThunderSpirit");
		
		thu.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try{
					my_field.ThunderSpirit();
					updatePet();
				}catch(NullPointerException ex){
					JOptionPane.showMessageDialog(null, "Please create a Summoner before summoning.");
				}
				
			}
		});
		buttonList.add(thu);
		JButton lig = new JButton("LightSpirit");
		
		lig.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try{
					my_field.LightSpirit();
					updatePet();
				}catch(NullPointerException ex){
					JOptionPane.showMessageDialog(null, "Please create a Summoner before summoning.");
				}
				
			}
		});
		buttonList.add(lig);
		JButton drk = new JButton("DarkSpirit");
		
		drk.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try{
					my_field.DarkSpirit();
					updatePet();
				}catch(NullPointerException ex){
					JOptionPane.showMessageDialog(null, "Please create a Summoner before summoning.");
				}
				
			}
		});
		buttonList.add(drk);
		JButton non = new JButton("No Pet");
		
		non.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				try{
					my_field.Release();
					updatePet();
				}catch(NullPointerException ex){
					JOptionPane.showMessageDialog(null, "Please create a Summoner before summoning.");
				}
				
			}
		});
		buttonList.add(non);
		
		
		for(JButton j : buttonList) {
			avatarBar.add(j);
		}
		my_display.add(avatarBar, BorderLayout.EAST);
	}

	/**
	 * 
	 * @param menuBar
	 * @return
	 */
	private JMenuBar setUpMenuItems(JMenuBar menuBar) {
		// File Menu Selections
		menuBar = setUpFile(menuBar);
		// Day Menu Selections
		menuBar = setUpDay(menuBar);
		// Weather Menu Selections
		menuBar = setUpWeather(menuBar);
		// Options menu selections
		menuBar = setUpOptions(menuBar);
		// Help menu selections
		menuBar = setUpHelp(menuBar);
		return menuBar;
	}

	/**
	 * 
	 * @param menuBar
	 * @return
	 */
	private JMenuBar setUpHelp(JMenuBar menuBar) {
		JMenu help = new JMenu("Help");
		menuBar.add(help);
		return menuBar;
	}

	/**
	 * 
	 * @param menuBar
	 * @return
	 */
	private JMenuBar setUpOptions(JMenuBar menuBar) {
		JMenu options = new JMenu("Options");
		JMenuItem reset_option = new JMenuItem("Reset Equiptment");
		reset_option.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if(my_field.hasCaller()){
					my_field.clearGearList();
					my_gear_list.setListData(my_field.getGearList());
					if(my_field.hasPet()) {
						updatePet();
					}
				}
			}
		});
		options.add(reset_option);
		menuBar.add(options);
		return menuBar;
	}

	/**
	 * 
	 * @param menuBar
	 * @return
	 */
	private JMenuBar setUpWeather(JMenuBar menuBar) {
		JMenu weather = new JMenu("Weather");
		final JCheckBoxMenuItem extraWeather = new JCheckBoxMenuItem("X2 Weather Effect");
		extraWeather.addActionListener(new ActionListener(){
			
			public void actionPerformed(ActionEvent e) {
				if(extraWeather.getState()){
					my_weather.setText(my_weather.getText() + " X2");
				}else {
					my_weather.setText(my_weather.getText().replace(" X2",""));
				}
			
			}
		});
		
		extraWeather.setSelected(false);
		
		JRadioButtonMenuItem fire_option = new JRadioButtonMenuItem("Firestorm");
		fire_option.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					my_field.setWeather(2, extraWeather.getState());
					if(extraWeather.getState()){
						my_weather.setText("Weather: Firestorm X2");
					}
					else {
						my_weather.setText("Weather: Firestorm");
					}
					if(my_field.hasCaller() && my_field.hasPet()) {
						updatePet();
					}
					
				}
			});
		JRadioButtonMenuItem earth_option = new JRadioButtonMenuItem("Sandstorm");
		earth_option.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					my_field.setWeather(3, extraWeather.getState());
					if(extraWeather.getState()){
						my_weather.setText("Weather: Sandstorm X2");
					}
					else {
						my_weather.setText("Weather: Sandstorm");
					}
					if(my_field.hasCaller() && my_field.hasPet()) {
						updatePet();
					}
					
				}
			});
		JRadioButtonMenuItem water_option = new JRadioButtonMenuItem("Rainstorm");
		water_option.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				my_field.setWeather(4, extraWeather.getState());
				if(extraWeather.getState()){
					my_weather.setText("Weather: Rainstorm X2");
				}
				else {
					my_weather.setText("Weather: Rainstorm");
				}
				if(my_field.hasCaller() && my_field.hasPet()) {
					updatePet();
				}
				
			}
		});
		JRadioButtonMenuItem wind_option = new JRadioButtonMenuItem("Windstorm");
		wind_option.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				my_field.setWeather(5, extraWeather.getState());
				if(extraWeather.getState()){
					my_weather.setText("Weather: Windstorm X2");
				}
				else {
					my_weather.setText("Weather: Windstorm");
				}
				if(my_field.hasCaller() && my_field.hasPet()) {
					updatePet();
				}
				
			}
		});
		JRadioButtonMenuItem ice_option = new JRadioButtonMenuItem("Icestorm");
		ice_option.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				my_field.setWeather(6, extraWeather.getState());
				if(extraWeather.getState()){
					my_weather.setText("Weather: Icestorm X2");
				}
				else {
					my_weather.setText("Weather: Icestorm");
				}
				if(my_field.hasCaller() && my_field.hasPet()) {
					updatePet();
				}
				
			}
		});
		JRadioButtonMenuItem thunder_option = new JRadioButtonMenuItem("Thunderstorm");
		thunder_option.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				my_field.setWeather(7, extraWeather.getState());
				if(extraWeather.getState()){
					my_weather.setText("Weather: Thunderstorm X2");
				}
				else {
					my_weather.setText("Weather: Thunderstorm");
				}
				if(my_field.hasCaller() && my_field.hasPet()) {
					updatePet();
				}
			}
		});
		JRadioButtonMenuItem light_option = new JRadioButtonMenuItem("Aurorastorm");
		light_option.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				my_field.setWeather(0, extraWeather.getState());
				if(extraWeather.getState()){
					my_weather.setText("Weather: Aurorastorm X2");
				}
				else {
					my_weather.setText("Weather: Aurorastorm");
				}
				if(my_field.hasCaller() && my_field.hasPet()) {
					updatePet();
				}
			}
		});
		JRadioButtonMenuItem dark_option = new JRadioButtonMenuItem("Voidstorm");
		dark_option.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				my_field.setWeather(1, extraWeather.getState());
				if(extraWeather.getState()){
					my_weather.setText("Weather: Voidstorm X2");
				}
				else {
					my_weather.setText("Weather: Voidstorm");
				}
				if(my_field.hasCaller() && my_field.hasPet()) {
					updatePet();
				}
			}
		});
		JRadioButtonMenuItem netural_option = new JRadioButtonMenuItem("No Weather");
		netural_option.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				extraWeather.setState(false);
				my_field.setWeather(8, extraWeather.getState());
				
				my_weather.setText("Weather: No Weather");
				
				
				if(my_field.hasCaller() && my_field.hasPet()) {
					updatePet();
				}
			}
		});
	
		ButtonGroup bg_weather = new ButtonGroup();
		weather = addElementsToMenu(weather, fire_option, bg_weather);
		weather = addElementsToMenu(weather,earth_option, bg_weather);
		weather = addElementsToMenu(weather,water_option, bg_weather);
		weather = addElementsToMenu(weather,wind_option, bg_weather);
		weather = addElementsToMenu(weather,ice_option, bg_weather);
		weather = addElementsToMenu(weather,thunder_option, bg_weather);
		weather = addElementsToMenu(weather,light_option, bg_weather);
		weather = addElementsToMenu(weather,dark_option, bg_weather);
		weather = addElementsToMenu(weather,netural_option, bg_weather);
		
		
		
		weather.add(extraWeather);
		menuBar.add(weather);
		return menuBar;
	}

	/**
	 * Constructs components for the day menu bar.
	 * 
	 * @param menuBar
	 * @return
	 */
	private JMenuBar setUpDay(JMenuBar menuBar) {
		final JMenu cday = new JMenu("Day");
		JRadioButtonMenuItem firesday= new JRadioButtonMenuItem("Firesday");
		firesday.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				my_field.setDay(2);
				day.setText("Day: Firesday");
				if(my_field.hasCaller() && my_field.hasPet()) {
					updatePet();
				}
			}
		});
		JRadioButtonMenuItem earthsday= new JRadioButtonMenuItem("Earthsday");
		earthsday.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				my_field.setDay(3);
				day.setText("Day: Earthsday");
				if(my_field.hasCaller() && my_field.hasPet()) {
					updatePet();
				}
			}
		});
		JRadioButtonMenuItem watersday= new JRadioButtonMenuItem("Watersday");
		watersday.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				my_field.setDay(4);
				day.setText("Day: Watersday");
				if(my_field.hasCaller() && my_field.hasPet()) {
					updatePet();
				}
			}
		});
		JRadioButtonMenuItem windsday= new JRadioButtonMenuItem("Windsday");
		windsday.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				my_field.setDay(5);
				day.setText("Day: Windsday");
				if(my_field.hasCaller() && my_field.hasPet()) {
					updatePet();
				}
			}
		});
		JRadioButtonMenuItem iceday= new JRadioButtonMenuItem("Iceday");
		iceday.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				my_field.setDay(6);
				day.setText("Day: Iceday");
				if(my_field.hasCaller() && my_field.hasPet()) {
					updatePet();
				}
			}
		});
		JRadioButtonMenuItem thundersday= new JRadioButtonMenuItem("Lightningsday");
		thundersday.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				my_field.setDay(7);
				day.setText("Day: Lightningsday");
				if(my_field.hasCaller() && my_field.hasPet()) {
					updatePet();
				}
			}
		});
		JRadioButtonMenuItem lightsday= new JRadioButtonMenuItem("Lightsday");
		lightsday.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				my_field.setDay(0);
				day.setText("Day: Lightsday");
				if(my_field.hasCaller() && my_field.hasPet()) {
					updatePet();
				}
			}
		});
		JRadioButtonMenuItem darksday= new JRadioButtonMenuItem("Darksday");
		darksday.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				my_field.setDay(1);
				day.setText("Day: Darksday");
				if(my_field.hasCaller() && my_field.hasPet()) {
					updatePet();
				}
			}
		});
		
		ButtonGroup bg_day = new ButtonGroup();
		addElementsToMenu(cday, firesday, bg_day);
		addElementsToMenu(cday, earthsday, bg_day);
		addElementsToMenu(cday, watersday, bg_day);
		addElementsToMenu(cday, windsday, bg_day);
		addElementsToMenu(cday, iceday, bg_day);
		addElementsToMenu(cday, thundersday, bg_day);
		addElementsToMenu(cday, lightsday, bg_day);
		addElementsToMenu(cday, darksday, bg_day);
		menuBar.add(cday);
		
		return menuBar;
	}

	/**
	 * Constructs components for the File menu option.
	 * 
	 * @param menuBar
	 * @return A completed Menu bar for File.
	 */
	private JMenuBar setUpFile(JMenuBar menuBar) {
		JMenu file = new JMenu("File");
		JMenuItem new_option = new JMenuItem("New");
		JMenuItem close_option = new JMenuItem("Close");
		file.add(new_option);
		file.add(close_option);
		menuBar.add(file);
		return menuBar;
	}

	/**
	 * Assigns menu options to the menu.
	 * 
	 * @param the_Menu
	 * @param the_option
	 * @param bg
	 * @return
	 */
	private JMenu addElementsToMenu(JMenu the_Menu, JRadioButtonMenuItem the_option, ButtonGroup bg) {
		bg.add(the_option);
		the_Menu.add(the_option);
		return the_Menu;
	}

	/**
	 * Aligns components for smn calculator's menu.
	 */
	private void setUpMenu() {
		JMenuBar menu_bar = new JMenuBar();
		menu_bar = setUpMenuItems(menu_bar);
		setJMenuBar(menu_bar);
		
	}
	
	/**
	 * Changes the displayed data related to Summoner's
	 */
	private void updateCaller() {
		name.setText("Name: " + my_field.getCallerName());
		level.setText("Level: " + my_field.getCallerLvl());
		mp.setText("Mp: " + my_field.getCallerMp());
		my_skill.setText("Smn Skill: " + my_field.getFinalSkill());
		pet_name.setText("Pet Name: None");
		perpetuation.setText("Perpetuation: 0");
		refresh.setText("Refresh: 0\n");
	}

	
	/**
	 * 
	 */
	private void updatePet() {
		pet_name.setText("Pet Name: " + my_field.getPet());
		refresh.setText("Refresh: " + my_field.getMpPerTic());
		perpetuation.setText("Perpetuation: " + my_field.getFinalPerp());
		siphon.setText("Siphon: " + my_field.getSiphon());
		
	}
	
	/**
	 * Starts the Gui for the summoner calculator.
	 */
	public void start() {
		setResizable(false);
		setPreferredSize(new Dimension(WIDTH, HEIGHT));
		pack();
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);	
	}
}
